<template>
  <el-input class="" v-model="filePathStr" placeholder="输入文件列表" />
  <el-input class="" v-model="exportName" placeholder="导出文件名" />

  <el-button @click="startExport">导出动画</el-button>

  <el-button @click="startExportImage">导出图片</el-button>

  <el-button @click="startExportJSON">重导SKE_JSON</el-button>

  <el-button @click="startExportJSONAniTimes">修改播放次数</el-button>

  <el-button @click="startExportMeta">修改预乘</el-button>

  <div class="console">
    <p v-for="(info, index) in infoList" :key="index">{{ info }}</p>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";
import { ElMessage } from "element-plus";
import { getAllFilePath } from "../utils/util";
const fs = window.require("fs");
const path = window.require("path");

const process = window.require("process");
const tinypngwrapper = (window as any).tinypngwrapper;

const electron = window.require("electron");
import robotjs_ipc, {
  ECommonColor,
  View,
  ViewColorFeature,
} from "../utils/robotjs_ipc";

export default class DragonBones extends Vue {
  infoList: string[] = [];
  filePathStr = "";
  exportName = "ske";
  views: View[] = [
    new View("首页", [
      new ViewColorFeature(613, 231, ECommonColor.Cyan),
      new ViewColorFeature(262, 692, ECommonColor.Green),
      new ViewColorFeature(1426, 688, ECommonColor.White),
      new ViewColorFeature(1482, 675, ECommonColor.Black),
      new ViewColorFeature(325, 749, ECommonColor.White),
    ]),
    new View("文件", [
      new ViewColorFeature(33, 36, ECommonColor.White),
      new ViewColorFeature(33, 127, ECommonColor.White),
      new ViewColorFeature(36, 333, ECommonColor.White),
      new ViewColorFeature(166, 92, ECommonColor.Blue),
      new ViewColorFeature(124, 251, ECommonColor.Blue),
    ]),
    new View("导入数据", [
      new ViewColorFeature(681, 446, ECommonColor.Green),
      new ViewColorFeature(672, 450, ECommonColor.Green),
      new ViewColorFeature(1156, 601, ECommonColor.Blue),
      new ViewColorFeature(729, 495, ECommonColor.White),
      new ViewColorFeature(1228, 445, ECommonColor.Blue),
    ]),
    new View("导入类型选择", [
      new ViewColorFeature(781, 467, ECommonColor.Green),
      new ViewColorFeature(890, 564, ECommonColor.White),
      new ViewColorFeature(978, 558, ECommonColor.White),
      new ViewColorFeature(1085, 462, ECommonColor.Blue),
      new ViewColorFeature(1012, 517, ECommonColor.White),
    ]),

    new View("骨架装配", [
      new ViewColorFeature(994, 1016, ECommonColor.White),
      new ViewColorFeature(1142, 990, ECommonColor.Gray),
      new ViewColorFeature(1577, 103, ECommonColor.White),
      // new ViewColorFeature(1638, 99, ECommonColor.Gray),
      // new ViewColorFeature(1506, 971, ECommonColor.Black),
      // new ViewColorFeature(394, 1024, ECommonColor.White),
    ]),
    new View("导出", [
      new ViewColorFeature(1211, 775, ECommonColor.Green),
      new ViewColorFeature(536, 178, ECommonColor.Green),
      new ViewColorFeature(870, 592, ECommonColor.Black),
      new ViewColorFeature(674, 460, ECommonColor.Black),
      new ViewColorFeature(1355, 709, ECommonColor.White),
    ]),
    new View("导出成功", [
      new ViewColorFeature(782, 467, ECommonColor.Green),
      new ViewColorFeature(779, 462, ECommonColor.Green),
      new ViewColorFeature(1099, 557, ECommonColor.Blue),
      new ViewColorFeature(1006, 465, ECommonColor.Blue),
      new ViewColorFeature(795, 570, ECommonColor.Blue),
    ]),
  ];

  getView(name: string) {
    return this.views.find((view) => {
      return view.name == name;
    });
  }

  async exportJSON(filePath: string) {
    fs.readFile(filePath, "utf-8", function (err: any, data: string) {
      if (err) {
        console.log("load path " + filePath + " error");
        return;
      }
      let _json = JSON.parse(data);
      const armatureList: any[] = _json.armature;
      console.log(_json);
      const nameList = armatureList.map((armature) => {
        return armature.name;
      });
      fs.writeFile(filePath, JSON.stringify(nameList), function (err: any) {
        if (err) {
          console.log("write json " + filePath + " faild");
          return;
        }
        // console.log('write json ' + item + ' success!');
      });
    });
  }
  async startExportJSON() {
    if (!fs.existsSync(this.filePathStr)) {
      ElMessage("文件或目录不存在");
      return;
    }

    let filePathList: string[] = [];
    getAllFilePath(this.filePathStr, filePathList, "ske.json");
    for (let index = 0; index < filePathList.length; index++) {
      const filePath = filePathList[index];
      await this.exportJSON(filePath);
      this.infoList.unshift(`完成：${filePath}`);
    }
    ElMessage({
      message: "文件导出完成",
      type: "success",
    });
  }

  async exportJSONAniTimes(filePath: string) {
    fs.readFile(filePath, "utf-8", function (err: any, data: string) {
      if (err) {
        console.log("load path " + filePath + " error");
        return;
      }
      let _json = JSON.parse(data);
      const armatureList: any[] = _json.armature;
      console.log(_json);
      armatureList.forEach((armature: any) => {
        armature.animation.forEach((ani: any) => {
          ani.playTimes = 0;
        });
      });
      fs.writeFile(filePath, JSON.stringify(_json), function (err: any) {
        if (err) {
          console.log("write json " + filePath + " faild");
          return;
        }
        // console.log('write json ' + item + ' success!');
      });
    });
  }
  async startExportJSONAniTimes() {
    if (!fs.existsSync(this.filePathStr)) {
      ElMessage("文件或目录不存在");
      return;
    }

    let filePathList: string[] = [];
    getAllFilePath(this.filePathStr, filePathList, "ske.json");
    for (let index = 0; index < filePathList.length; index++) {
      const filePath = filePathList[index];
      await this.exportJSONAniTimes(filePath);
      this.infoList.unshift(`完成：${filePath}`);
    }
    ElMessage({
      message: "文件导出完成",
      type: "success",
    });
  }

  async exportMeta(filePath: string) {
    fs.readFile(filePath, "utf-8", function (err: any, data: string) {
      if (err) {
        console.log("load path " + filePath + " error");
        return;
      }
      let _json = JSON.parse(data);
      _json.premultiplyAlpha = true;

      fs.writeFile(filePath, JSON.stringify(_json), function (err: any) {
        if (err) {
          console.log("write json " + filePath + " faild");
          return;
        }
      });
    });
  }
  async startExportMeta() {
    if (!fs.existsSync(this.filePathStr)) {
      ElMessage("文件或目录不存在");
      return;
    }

    let filePathList: string[] = [];
    getAllFilePath(
      this.filePathStr,
      filePathList,
      `${this.exportName}_tex.png.meta`
    );
    for (let index = 0; index < filePathList.length; index++) {
      const filePath = filePathList[index];
      await this.exportMeta(filePath);
      this.infoList.unshift(`完成：${filePath}`);
    }
    ElMessage({
      message: "文件导出完成",
      type: "success",
    });
  }

  async startExport() {
    // robotjs_ipc.setKeyboardDelay(100);
    // const pathList = JSON.parse(this.filePathStr);
    // for (let index = 0; index < pathList.length; index++) {
    //   const filePath = pathList[index];
    //   await this.exportBin(filePath);
    //   this.infoList.unshift(`完成：${filePath}`)
    // }
    if (!fs.existsSync(this.filePathStr)) {
      ElMessage("文件或目录不存在");
      return;
    }

    let filePathList: string[] = [];
    getAllFilePath(this.filePathStr, filePathList, "ske.json");
    console.log(filePathList);

    for (let index = 0; index < filePathList.length; index++) {
      const filePath = filePathList[index];
      await this.exportBin(filePath);
      this.infoList.unshift(`完成：${filePath}`);
    }
  }
  async exportBin(filePath: string) {
    await this.getView("首页")?.waitToActive();
    await robotjs_ipc.moveMouse(44, 18);
    await robotjs_ipc.mouseClick();
    await this.getView("文件")?.waitToActive();
    await robotjs_ipc.moveMouse(82, 190);
    await robotjs_ipc.mouseClick();
    await this.getView("导入数据")?.waitToActive();
    await robotjs_ipc.moveMouse(844, 489);
    await robotjs_ipc.mouseClick();

    //await robotjs_ipc.delay(1000)
    await robotjs_ipc.typeString(filePath);
    await robotjs_ipc.delay(400);
    //点击纹理类型：纹理集
    await robotjs_ipc.moveMouse(762, 569);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(200);
    //点击纹理集输入框
    await robotjs_ipc.moveMouse(788, 600);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(400);
    await robotjs_ipc.typeString(`${path.parse(filePath).dir}/tex.png`);
    await robotjs_ipc.delay(500);
    //点击配置文件输入框
    await robotjs_ipc.moveMouse(813, 631);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(100);
    await robotjs_ipc.typeString(`${path.parse(filePath).dir}/tex.json`);
    await robotjs_ipc.delay(500);
    //点击完成
    await robotjs_ipc.moveMouse(1088, 729);
    await robotjs_ipc.mouseClick();

    const isExportTypeView = await this.getView("导入类型选择")?.waitToActive(
      100,
      1000
    );
    if (isExportTypeView) {
      await robotjs_ipc.moveMouse(962, 562);
      await robotjs_ipc.mouseClick();
    }

    await this.getView("骨架装配")?.waitToActive();
    await robotjs_ipc.moveMouse(44, 18);
    await robotjs_ipc.mouseClick();
    await this.getView("文件")?.waitToActive();
    await robotjs_ipc.moveMouse(51, 238);
    await robotjs_ipc.mouseClick();
    await this.getView("导出")?.waitToActive();
    //选择二进制
    // await robotjs_ipc.moveMouse(727, 336);
    // await robotjs_ipc.mouseClick();
    // await robotjs_ipc.delay(500);
    // await robotjs_ipc.moveMouse(732, 386);
    // await robotjs_ipc.mouseClick();
    // await robotjs_ipc.delay(300);

    await robotjs_ipc.moveMouse(765, 533);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(500);
    await robotjs_ipc.moveMouse(715, 584);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(300);

    await robotjs_ipc.moveMouse(752, 562);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(300);
    await robotjs_ipc.keyTap("a", "control");
    await robotjs_ipc.delay(200);
    await robotjs_ipc.typeString(`${path.parse(filePath).dir}`);
    await robotjs_ipc.delay(500);
    await robotjs_ipc.moveMouse(706, 594);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(300);
    await robotjs_ipc.typeString(this.exportName);
    await robotjs_ipc.delay(500);
    await robotjs_ipc.moveMouse(882, 848);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(1000);
    //await this.getView("导出成功")?.waitToActive();
    await robotjs_ipc.moveMouse(962, 562);
    await robotjs_ipc.mouseClick();
    await this.getView("骨架装配")?.waitToActive();

    await robotjs_ipc.keyTap("w", "control");
    await robotjs_ipc.delay(600);
    await robotjs_ipc.moveMouse(963, 564);
    await robotjs_ipc.mouseClick();
  }

  async startExportImage() {
    if (!fs.existsSync(this.filePathStr)) {
      ElMessage("文件或目录不存在");
      return;
    }

    let filePathList: string[] = [];
    getAllFilePath(this.filePathStr, filePathList, "ske.json");
    console.log(filePathList);

    for (let index = 0; index < filePathList.length; index++) {
      const filePath = filePathList[index];
      await this.exportImage(filePath);
      this.infoList.unshift(`完成：${filePath}`);
    }
  }
  async exportImage(filePath: string) {
    await this.getView("首页")?.waitToActive();
    await robotjs_ipc.moveMouse(44, 18);
    await robotjs_ipc.mouseClick();
    await this.getView("文件")?.waitToActive();
    await robotjs_ipc.moveMouse(82, 190);
    await robotjs_ipc.mouseClick();
    await this.getView("导入数据")?.waitToActive();
    await robotjs_ipc.moveMouse(844, 489);
    await robotjs_ipc.mouseClick();

    //await robotjs_ipc.delay(1000)
    await robotjs_ipc.typeString(filePath);
    await robotjs_ipc.delay(400);
    //点击纹理类型：纹理集
    await robotjs_ipc.moveMouse(762, 569);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(200);
    //点击纹理集输入框
    await robotjs_ipc.moveMouse(788, 600);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(400);
    await robotjs_ipc.typeString(`${path.parse(filePath).dir}/tex.png`);
    await robotjs_ipc.delay(500);
    //点击配置文件输入框
    await robotjs_ipc.moveMouse(813, 631);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(100);
    await robotjs_ipc.typeString(`${path.parse(filePath).dir}/tex.json`);
    await robotjs_ipc.delay(500);
    //点击完成
    await robotjs_ipc.moveMouse(1088, 729);
    await robotjs_ipc.mouseClick();

    const isExportTypeView = await this.getView("导入类型选择")?.waitToActive(
      100,
      1000
    );
    if (isExportTypeView) {
      await robotjs_ipc.moveMouse(962, 562);
      await robotjs_ipc.mouseClick();
    }

    await this.getView("骨架装配")?.waitToActive();
    await robotjs_ipc.moveMouse(44, 18);
    await robotjs_ipc.mouseClick();
    await this.getView("文件")?.waitToActive();
    await robotjs_ipc.moveMouse(51, 238);
    await robotjs_ipc.mouseClick();
    await this.getView("导出")?.waitToActive();
    //选择二进制
    // await robotjs_ipc.moveMouse(727, 336);
    // await robotjs_ipc.mouseClick();
    // await robotjs_ipc.delay(500);
    // await robotjs_ipc.moveMouse(732, 386);
    // await robotjs_ipc.mouseClick();
    // await robotjs_ipc.delay(300);

    //点击图片
    await robotjs_ipc.moveMouse(794, 220);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(500);
    await robotjs_ipc.moveMouse(750, 314);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(500);
    await robotjs_ipc.moveMouse(754, 362);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(500);
    //选择项目所在文件夹
    await robotjs_ipc.moveMouse(739, 583);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(500);
    await robotjs_ipc.moveMouse(730, 635);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(300);

    await robotjs_ipc.moveMouse(792, 615);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(300);
    await robotjs_ipc.keyTap("a", "control");
    await robotjs_ipc.delay(200);
    await robotjs_ipc.typeString(`${path.parse(filePath).dir}`);
    await robotjs_ipc.delay(500);
    //点击完成
    await robotjs_ipc.moveMouse(876, 854);
    await robotjs_ipc.mouseClick();
    await robotjs_ipc.delay(1000);
    //await this.getView("导出成功")?.waitToActive();
    await robotjs_ipc.moveMouse(962, 562);
    await robotjs_ipc.mouseClick();
    await this.getView("骨架装配")?.waitToActive();

    await robotjs_ipc.keyTap("w", "control");
    await robotjs_ipc.delay(600);
    await robotjs_ipc.moveMouse(963, 564);
    await robotjs_ipc.mouseClick();
  }
}
</script>

<style scoped>
.console {
  max-height: 150px;
  overflow-y: scroll;
}
.dropActive1 {
  background-color: red;
  border-color: red;
}
</style>

function getAllFilePath() {
  throw new Error("Function not implemented.");
}
